import {
  createRouter,
  createWebHistory,
  // createWebHashHistory
} from "vue-router"; 
import routes from './routes'
import {useStore} from "../pinia/login"
// 无需登录即可访问的路由
const initRoutes = [{
    path: "/",
    redirect: "/home",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  }, 
]; 
 
const router = createRouter({
  history: createWebHistory(),
  routes: [
    ...initRoutes,
    ...routes
  ]
}); 

// 对路由进行拦截, 根据条件放行或跳转
router.beforeEach((to, from, next) => {
 let store = useStore();

 if (to.path === '/login') {
  next();
 } else {
  // 判断一下是否已登录, 没登录跳转到登录
  if (store.token) {
   next();
  } else {
   next('/login')
  }
 }
});

export default router;